package com.transfer.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.staff.entity.Staff;
import com.staff.service.StaffService;
import com.transfer.dao.TitleAppointRepository;
import com.transfer.entity.TitleAppoint;
import com.transfer.entity.dto.TitleAppointQueryDTO;

@Service
@Transactional
public class TitleAppointService implements ITitleAppointService {
	@Autowired
	private TitleAppointRepository titleAppointRepository;
	@Autowired
	private ITitleAppointService titleAppointService;
	@Autowired
	private StaffService staffService;
	
	@Override
	public void saveOrUpdate(TitleAppoint titleAppoint) {
		titleAppointRepository.save(titleAppoint);
	}

	@Override
	public void delete(TitleAppoint titleAppoint) {
		titleAppoint=titleAppointRepository.findOne(titleAppoint.getId());
		if(titleAppoint!=null) {
			titleAppoint.setStaff(null);
			titleAppointRepository.save(titleAppoint);
		}
		titleAppointRepository.delete(titleAppoint);
	}

	@Override
	public TitleAppoint findOne(Long id) {
		return titleAppointRepository.findOne(id);
	}

	@Override
	public List<TitleAppoint> findAll() {
		return (List<TitleAppoint>) titleAppointRepository.findAll();
	}

	@Override
	public void delete(List<Long> ids) {
		List<TitleAppoint> titleAppointList = (List<TitleAppoint>)titleAppointRepository.findAll(ids);
		for(TitleAppoint titleAppoint : titleAppointList) {
			titleAppoint.setStaff(null);
			titleAppointRepository.save(titleAppoint);
		}
			titleAppointRepository.delete(titleAppointList);
		
	}

	@Override
	public Page<TitleAppointQueryDTO> findAll(Specification<TitleAppoint> spec, Pageable pageable) {
		//将离职信息page转化为离职信息DTOpage
		List<TitleAppointQueryDTO> results = new ArrayList<TitleAppointQueryDTO>();
		Page<TitleAppoint> page = titleAppointRepository.findAll(spec, pageable);
		for(TitleAppoint sc:page.getContent()) {
			TitleAppointQueryDTO scDTO = new TitleAppointQueryDTO(sc, sc.getStaff());
        	results.add(scDTO);
		}
		return new PageImpl<TitleAppointQueryDTO>(results, pageable, results.size());
	}

	@Override
	public void saveOrUpdate(TitleAppointQueryDTO titleAppointQueryDTO) {
		// TODO Auto-generated method stub
		TitleAppoint sr = new TitleAppoint();
		if(titleAppointQueryDTO.getStaffNum() != null) {
			Staff staff = staffService.findOne(titleAppointQueryDTO.getStaffNum());
			if(titleAppointQueryDTO.getStaffId() != null) {
				sr = titleAppointService.findOne(titleAppointQueryDTO.getId());
			}else {
				//
			}
			sr.setStaffNum(titleAppointQueryDTO.getStaffNum());
			sr.setStaffName(titleAppointQueryDTO.getStaffName());
			sr.setAppointDate(titleAppointQueryDTO.getAppointDate());
			sr.setAppointTitle(titleAppointQueryDTO.getAppointTitle());			
			sr.setStaff(staff);
			
			titleAppointService.saveOrUpdate(sr);
		}else {
			//
		}
	}

}
